﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Cms.Web.Framework.Controls;
using iLib.Web.UI;

namespace Cms.Web.UI.Admin
{
    public partial class Categories : AdminPage, ICategoriesView
    {
        private CategoriesPresenter _presenter;

        #region ICategoriesView Members

        public void SetCategories(IList<Category> categories)
        {
            IList<Category> itemTree = new List<Category>();
            RenderItems(ref itemTree, categories);
            rpCategories.DataSource = itemTree;
            rpCategories.DataBind();
            lblNoCategories.Visible = rpCategories.Items.Count == 0;
        }

        public string ContextTranslationFile
        {
            get { return "Admin/Translations/Categories.xml"; }
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new CategoriesPresenter(this);
            HeadingText = GetText(this, "Heading");
            if (!Page.IsPostBack)
            {
                _presenter.Initialize();
                HeadingIcon = litIcon.Text;
            }
            lbDelete.OnClientClick = "return cms.Dialogs.ConfirmLinkButton(this, " +
                                        "function (status) { " +
                                            "if (status == 'ok') " +
                                                "cms.Lists.ShowPaneOverlay('categoryPaneOverlay');" +
                                            "else " +
                                                "cms.Lists.HidePaneOverlays(); " +
                                        "}, " +
                                        "'" + GetText(this, "DeleteHeader") + "', " +
                                        "'" + GetText(this, "DeleteMessage") + "', " +
                                        "'" + GetText(this, "DeleteOK") + "', " +
                                        "'" + GetText(this, "DeleteCancel") + "', " +
                                        "true, " +
                                        "cms.Lists.CurrentName);";
        }

        protected void rpCategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            var category = (Category) e.Item.DataItem;
            var litID = (Literal) e.Item.FindControl("litID");
            var litName = (Literal) e.Item.FindControl("litName");
            var litCategoryID = (Literal)e.Item.FindControl("litCategoryID");
            litID.Text = @"<div style=""margin-left:" + (category.GetLevelInTree() * 16) + @"px;""><span id=""C" + category.ID + @""">";
            litName.Text = category.Name;
            litCategoryID.Text = category.ID.ToString();
        }

        protected void lbDragEnd_Click(object sender, EventArgs e)
        {
            if (hidDropzoneID.Value != string.Empty)
                _presenter.Move(hidDraggedID.Value, hidDropzoneID.Value, hidDropzonePart.Value);
            UpdateCategoriesPane();
        }
        
        protected void lbDelete_Click(object sender, EventArgs e)
        {
            _presenter.Delete(int.Parse(hidDeleteID.Value.Substring(1)));
            UpdateCategoriesPane();
        }

        private void UpdateCategoriesPane()
        {
            upnlCategories.Update();
            ClientScriptProxy.Current.RegisterStartupScript(pnlActions, GetType(), "initList", "initList();", true);
        }

        private void RenderItems(ref IList<Category> itemTree, IList<Category> items)
        {
            if (items.Count == 0)
                return;
            foreach (Category mi in items)
            {
                itemTree.Add(mi);
                RenderItems(ref itemTree, mi.SubCategories);
            }
        }
    }
}